package com.simple.orm.agreement.utils;

import com.alibaba.fastjson.JSON;

import java.io.DataInputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Objects;

public class DataInputStreamUtil {

    public static <T> T readData(DataInputStream dataInputStream, Field field) throws IOException {
        Class<T> type = (Class<T>) field.getType();
        Object t = null;
        if (Objects.equals(byte.class, type) || Objects.equals(Byte.class, type)) {
            t = dataInputStream.readByte();
        } else if (Objects.equals(short.class, type) || Objects.equals(Short.class, type)) {
            t = dataInputStream.readShort();
        } else if (Objects.equals(char.class, type) || Objects.equals(Character.class, type)) {
            t = dataInputStream.readChar();
        } else if (Objects.equals(int.class, type) || Objects.equals(Integer.class, type)) {
            t = dataInputStream.readInt();
        } else if (Objects.equals(long.class, type) || Objects.equals(Long.class, type)) {
            t = dataInputStream.readLong();
        } else if (Objects.equals(float.class, type) || Objects.equals(Float.class, type)) {
            t = dataInputStream.readFloat();
        } else if (Objects.equals(double.class, type) || Objects.equals(Double.class, type)) {
            t = dataInputStream.readDouble();
        } else if (Objects.equals(boolean.class, type) || Objects.equals(Boolean.class, type)) {
            t = dataInputStream.readBoolean();
        } else if (Objects.equals(String.class, type)) {
            int len = dataInputStream.readInt();
            byte[] data = new byte[len];
            dataInputStream.readFully(data);
            t = new String(data, StandardCharsets.UTF_8);
        } else if (Objects.equals(LocalDateTime.class, type)) {
            int len = dataInputStream.readInt();
            byte[] data = new byte[len];
            dataInputStream.readFully(data);
            String cache = new String(data, StandardCharsets.UTF_8);
            t = LocalDateTime.parse(cache, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        } else if (Objects.equals(Date.class, type)) {
            int len = dataInputStream.readInt();
            byte[] data = new byte[len];
            dataInputStream.readFully(data);
            String cache = new String(data, StandardCharsets.UTF_8);
            try {
                t = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(cache);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        } else if (Objects.equals(byte[].class, type)) {
            int len = dataInputStream.readInt();
            t = new byte[len];
            dataInputStream.readFully((byte[]) t);
        } else {
            int len = dataInputStream.readInt();
            byte[] data = new byte[len];
            dataInputStream.readFully(data);
            String cache = new String(data, StandardCharsets.UTF_8);
            t = JSON.parseObject(cache, type);
        }
        return (T) t;
    }
}
